/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.mobileWeb.project;

import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.common.persistence.Page;
import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.enroll.EduEnroll;
import com.idenger.modules.edu.entity.project.EduProject;
import com.idenger.modules.edu.entity.teacher.EduTeacherInfo;
import com.idenger.modules.edu.entity.user.EduUser;
import com.idenger.modules.edu.service.course.EduCourseService;
import com.idenger.modules.edu.service.enroll.EduEnrollService;
import com.idenger.modules.edu.service.project.EduProjectCourseService;
import com.idenger.modules.edu.service.project.EduProjectService;
import com.idenger.modules.edu.service.teacher.EduTeacherInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目Controller
 * @author chenlin
 * @version 2017-12-04
 */
@Controller
@RequestMapping(value = "${mobilePath}/edu/project/eduProject")
public class MEduProjectController extends BaseController {

	@Autowired
	private EduProjectService eduProjectService;
	@Autowired
	private EduProjectCourseService eduProjectCourseService;
	@Autowired
	private EduCourseService eduCourseService;
	@Autowired
	private EduTeacherInfoService eduTeacherInfoService;
	@Autowired
	private EduEnrollService eduEnrollService;

	@ModelAttribute
	public EduProject get(@RequestParam(required = false) String id) {
		EduProject entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = eduProjectService.get(id);
		}
		if (entity == null) {
			entity = new EduProject();
		}
		return entity;
	}

	@RequestMapping(value = "projectList")
	public String projectList(EduProject eduProject, HttpServletRequest request, HttpServletResponse response, Model model) {
		BaseResponseMsg baseResponseMsg;
		eduProject.setOpen("1");
		Page<EduProject> page = eduProjectService.findPage(new Page<EduProject>(request, response), eduProject);
		if(page.getList().size()>0){
			List<EduProject> list = page.getList();
			for(int i =0;i<list.size();i++){
				EduProject project =list.get(i);//项目
				//查询这个项目下的课程
				List<EduCourse> eduCourseList = eduCourseService.findProjectContentCourse(project.getId());
				//查询课程下的老师
				if(eduCourseList.size()>0){
					List<EduTeacherInfo> eduTeacher = eduTeacherInfoService.findCourseTeacherDetailByCourseId(eduCourseList.get(0).getId());
					if(eduTeacher.size()>0){
						project.setEduTeacherInfo(eduTeacher.get(0));
					}
				}
			}
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("page",page);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("None");
		}
		return renderString(response, baseResponseMsg);
	}

	//查找项目详情
	@RequestMapping(value = "findProjectDetailById")
	public String findProjectDetailById(EduProject eduProject,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		if(eduProject!=null){
			//查询项目下的课程
			List<EduCourse> eduCourseList = eduCourseService.findProjectContentCourse(eduProject.getId());
			if(eduCourseList.size()>0){
				EduCourse eduCourse = eduCourseList.get(0);
				List<EduTeacherInfo> eduTeacher = eduTeacherInfoService.findCourseTeacherDetailByCourseId(eduCourseList.get(0).getId());
				if(eduTeacher.size()>0){
					eduCourse.setEduTeacher(eduTeacher);
				}
			}
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			Map<String, Object> map = new HashMap<>();
			map.put("eduProject",eduProject);
			map.put("eduCourseList",eduCourseList);
			baseResponseMsg.addData(map);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("None");
		}
		return renderString(response, baseResponseMsg);
	}
	
	//查找我的项目详情
	@RequestMapping(value = "findMyProjectDetail")
	public String findMyProjectDetail(EduProject eduProject,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser eduUser = (EduUser) request.getAttribute("currentUser");
		if(eduProject!=null){
			//查询项目下的课程
			List<EduCourse> eduCourses = eduCourseService.findProjectContentCourse(eduProject.getId());
			List<EduCourse> eduCoursesList = new ArrayList<>();
			if(eduCourses.size()>0){
				for(int i=0;i<eduCourses.size();i++){
					EduCourse eduCourse = eduCourses.get(i);
					String coursePayStatus = whethPayCourse(eduUser.getId(), eduCourse.getId());
					eduCourse.setCoursePayStatus(coursePayStatus);
					List<EduTeacherInfo> eduTeacher = eduTeacherInfoService.findCourseTeacherDetailByCourseId(eduCourses.get(0).getId());
					if(eduTeacher.size()>0){
						eduCourse.setEduTeacher(eduTeacher);
					}
					eduCoursesList.add(eduCourse);
				}
			}
			 
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			Map<String, Object> map = new HashMap<>();
			map.put("eduProject",eduProject);
			map.put("eduCourseList",eduCoursesList);
			//订单的状况
			map.put("payStatus",findwhethPayProject(eduUser.getId(), eduProject.getId()));
			baseResponseMsg.addData(map);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("None");
		}
		return renderString(response, baseResponseMsg);
	}
	
	public String findwhethPayProject(String userId ,String projectId){
		List<EduEnroll> list =  eduEnrollService.findwhethPayProject(projectId, userId);
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	/***
	 * 是否购买课程
	 * @param userId
	 * @param courseId
	 * @return
	 */
	public String whethPayCourse(String userId,String courseId){
		EduEnroll eduEnroll =  eduEnrollService.findwhethPayCourse(courseId, userId);
		if(eduEnroll == null){
			return "0";
		}
		return "1";
	}
	
}